package com.liu.hadoop.clickStreamETL.mapreduce;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Partitioner;

/**
 * @Description: 分区类,做完hash后取模
 * @author 刘章程
 */
public class SessionIdPartioner extends Partitioner<Text, Text> {

	/* (non-Javadoc)
	 * @see org.apache.hadoop.mapreduce.Partitioner#getPartition(java.lang.Object, java.lang.Object, int)
	 */
	@Override
	public int getPartition(Text key2, Text value, int numPartitions) {
		// 默认值
		String sessionId = "-";
		
		if (key2 != null) {
			sessionId = key2.toString().split("&")[0];
		}
		
		// 设置分区数数量(设置对应的哪个分区)
		int num = (sessionId.hashCode() & Integer.MAX_VALUE) % numPartitions;
		
		return num;
	}

}
